package vn.mobiistar.videoapp.activity;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.fragment.BaseFragment;
import vn.mobiistar.videoapp.fragment.CategoryFragment;
import vn.mobiistar.videoapp.fragment.IndexFragment;
import vn.mobiistar.videoapp.fragment.SearchFragment;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

public class MainActivity extends BaseMenuActivity {


	private static final String TAG = "MainActivity";
	public  IndexFragment 			  indexFragment;
	public  BaseFragment currentFragment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if (savedInstanceState == null) {
			indexFragment 	= new IndexFragment(this);
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.add(R.id.ma_container, indexFragment);
			// Commit the transaction
			transaction.commit();
        }
	}
	@Override
	public void onClick_GoHomePage(View paramView){
		if (savedInstanceState == null) {
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.ma_container, indexFragment);
			// Commit the transaction
			transaction.commit();
		}
		navSearchButton.setVisibility(View.VISIBLE);
		navSearchText.setVisibility(View.GONE);
		navSearchRemoveButton.setVisibility(View.GONE);
		setTitle(getResources().getString(R.string.left_menu_home));
		showContent();
	}
	
	@Override
	public void onClick_GoCategoryListPage(View paramView){
		if(savedInstanceState == null){
			CategoryFragment fragment 	= new CategoryFragment(this);
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.ma_container, fragment);
			// Commit the transaction
			transaction.commit();
		}
		navSearchButton.setVisibility(View.VISIBLE);
		navSearchText.setVisibility(View.GONE);
		navSearchRemoveButton.setVisibility(View.GONE);
		setTitle(getResources().getString(R.string.left_menu_category));
		showContent();
	}
	public void onClick_Search(View paramView){
		if(savedInstanceState == null){
			SearchFragment fragment 	= new SearchFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.ma_container, fragment);
			// Commit the transaction
			transaction.commit();
			navSearchButton.setVisibility(View.GONE);
			navSearchText.setVisibility(View.VISIBLE);
			navSearchRemoveButton.setVisibility(View.VISIBLE);
		}
		showContent();
	}
	
	private Fragment getCurrentFragment(){
	    FragmentManager fragmentManager = getSupportFragmentManager();
	    String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
	    Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
	    return currentFragment;
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		Toast.makeText(getApplicationContext(), "dsfasdfasdf", Toast.LENGTH_SHORT).show();
		Log.i(TAG, " Back Pressed ");
		BaseFragment fragmentBeforeBackPress = (BaseFragment) currentFragment;
		if(fragmentBeforeBackPress.getPlayer() != null && fragmentBeforeBackPress.getPlayer().getYoutubePlayer() != null){
			Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
			int rotation = display.getRotation();
			if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270 ) {
				// neu dang o che do man hinh nam ngang, thi dung video lai va 
				fragmentBeforeBackPress.getPlayer().getYoutubePlayer().pause();
				fragmentBeforeBackPress.getPlayer().getYoutubePlayer().setFullscreen(false);
				fragmentBeforeBackPress.getPlayer().getDraggablePanel().setVisibility(View.GONE);
			}else{
				if(fragmentBeforeBackPress.getPlayer().getDraggablePanel()!=null)
        			if(fragmentBeforeBackPress.getPlayer().getDraggablePanel().isMaximized()) 
        				fragmentBeforeBackPress.getPlayer().getDraggablePanel().minimize();
			}
		}
		
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            Log.i(TAG,"Changed to  LANDSCAPE");
            // neu dang xem video o che do man hinh doc, va xoay man hinh sang che do nam ngang 
            // thi phong lon video fullscreen
            if(currentFragment.mPlayer.getDraggablePanel()!= null)if(currentFragment.mPlayer.getDraggablePanel().getVisibility() == View.VISIBLE) if(currentFragment.mPlayer.getYoutubePlayer() != null){
            	currentFragment.mPlayer.getYoutubePlayer().setFullscreen(true);
            }
        }else{
            Log.i(TAG,"Changed to  PORTRAIT");
        }
    }
}